<?php
$this->breadcrumbs=array(
	$model->project->name=>array('project/status','id'=>$model->project->id),
	Yii::t('project','Project Subdivision')=>array('project/sub','id'=>$model->project->id),
	$model->name=>array('status','id'=>$model->id),
	Yii::t('project','Unit Project Subdivision'),
);
?>

<?php echo $this->renderPartial('_wizard',array('model'=>$model, 'current'=>'sub')); ?>

<?php echo CHtml::link(Yii::t('project','Create Segment Project'), array('segmentProject/create', 'uid'=>$model->id), array('class'=>'btn btn-primary')); ?>
<?php 
$dataProvider=new CActiveDataProvider('SegmentProject', array(
		'criteria'=>array(
				'condition'=>'unitproject_id=' . $model->id,
				'order'=>'modify_at DESC',
		),
		'pagination'=>array(
				'pageSize'=>5,
		),
));

$this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'segment-project-grid',
	'dataProvider'=>$dataProvider,
	'columns'=>array(
		array(
			'name' => 'name',
			'header'=>Yii::t('project','Segment Project Name'),
		),
// 		array(
// 			'name' => 'description',
// 			'header'=>Yii::t('project','Segment Project Description'),
// 		),
		array(
			'name' => 'real_invest_used',
			'header'=>Yii::t('project','Segment Poject Status'),
			'type' => 'raw',
			'value'=>function($data){
						if($data->end_work_status == 1) {
							if($data->quality == 0) {
								$qualityContent = '<p class="text-info">' . Yii::t('project','Qualified') . '</p>';
							} else {
								$qualityContent = '<p class="text-success">' . Yii::t('project','Good') . '</p>';
							}
						} else {
							$qualityContent = '<p class="muted">' . Yii::t('project','Segment Project Not Finished') . '</p>';
						}
						return $qualityContent;
					},
		),
		array(
				'name' => 'real_invest_used',
				'header'=>Yii::t('project','Segment Poject Safety Status'),
				'type' => 'raw',
				'value'=>function($data){
					if($data->start_work_status == 1) {
						if($data->safety == 0) {
							$safetyContent = '<p class="text-success">' . Yii::t('project','No Sefety Issue') . '</p>';
						} else {
							$safetyContent = '<p class="text-error">' . Yii::t('project','Has Sefety Issue') . ' - ' . $data->safety_details . '</p>';
						}
					} else {
						$safetyContent = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
					}
					return $safetyContent;
				},
		),
		array(
			'name' => 'real_invest_used',
			'header'=>Yii::t('project','Investment'),
			'type' => 'raw',
			'value'=>function($data){
						$investProgress = '0%';
						$progressBarClass = 'progress-success';
						$investProgressStr = '0%';
						if($data->real_invest_used != 0 && $data->plan_invest != 0) {
							$investProgress = strval(round(($data->real_invest_used/$data->plan_invest), 2) * 100) . '%';
							$investProgressStr = $investProgress;
							
							if(($data->real_invest_used/$data->plan_invest) > 1) {
								$investProgress = '100%';	//Over 100% is meaningless
								$progressBarClass = 'progress-danger';
							}
						}
						
						
						return '<div class="progress ' . $progressBarClass . ' progress-striped" style="margin-bottom: 9px;">
									<div class="bar" style="width:' . $investProgress . '">' . $investProgressStr . '</div>
								</div>';
					},
		),
		array(
				'name' => 'pre_start_at',
				'header'=>Yii::t('project','Schedule'),
				'type' => 'raw',
				'value'=>function($data){
								if(strval($data->real_start_at) == '0000-00-00 00:00:00')
									$contentCssClass = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
								else {
									if(strval($data->real_end_at) == '0000-00-00 00:00:00') {
										if(strtotime($data->pre_end_at) < time())
											$contentCssClass = '<p class="text-error">' . Yii::t('project','Pending-Overtime') . '</p>';
										else 
											$contentCssClass = '<p class="text-info">' . Yii::t('project','Pending') . '</p>';
									} else {
										if(strtotime($data->real_end_at) > strtotime($data->pre_end_at))
											$contentCssClass = '<p class="text-error">' . Yii::t('project','Finished-Overtime') . '</p>';
										else
											$contentCssClass = '<p class="text-success">' . Yii::t('project','Finished-Ontime') . '</p>';
									}
									
								}
								
								return $contentCssClass;
							},
		),
		array(
				'class'=>'bootstrap.widgets.TbButtonColumn',
				'template'=>'{update}',
				'htmlOptions'=>array('width'=>'80px'),
				//'template'=>'{view}{update}{delete}',
				'buttons'=>array
				(
					'update' => array('url'=>'$this->grid->controller->createUrl("/segmentProject/status", array("id"=>$data["id"]))'),
				),
		),
	),
));
?>

<div class="row-fluid">
	<div class="span12">
		<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
							'id'=>'unitproject-sub-form',
							'enableAjaxValidation'=>false,
		)); ?>
		
		<?php echo $form->hiddenField($model,'id'); ?>

		<div class="form-actions">
			<?php $this->widget('bootstrap.widgets.TbButton', array(
				'buttonType'=>'link',
				'type'=>'link',
				'url'=>array('unitProject/status', 'id'=>$model->id),
				'label'=>Yii::t('common','Cancel'),
			)); ?>
			<?php $this->widget('bootstrap.widgets.TbButton', array(
				'buttonType'=>'submit',
				'type'=>'primary',
				'label'=>Yii::t('common','Save and Next'),
			)); ?>
		</div>
	
		<?php $this->endWidget(); ?>
	</div>
</div>